package cn.sugarscat.account;

import cn.sugarscat.db.*;
import cn.sugarscat.util.Module;

import java.sql.ResultSet;
import java.sql.SQLException;

public class AccountManager {

    public static boolean checkUname(String uname) {
        DB db = new DB();
        boolean ok;
        String sql = "SELECT * FROM `user` WHERE `user_uname` = ?";
        ResultSet res = db.executeQuery(sql, uname);
        try {
            ok = res.next();
            res.close();
            db.closeDB();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return ok;
    }

    public static boolean register(User user) {
        DB db = new DB();
        boolean ok = false;
        String sql = "INSERT INTO `user`(" +
                "`user_uname`, " +
                "`user_password`, " +
                "`user_name`, " +
                "`user_email`, " +
                "`user_contact_info`) VALUES ('" +
                user.getUname() + "', '" +
                user.getPassword() + "', '" +
                user.getName() + "', '" +
                user.getEmail() + "', '" +
                user.getContactInfo() +  "');";
        int num = db.executeInsert(sql);
        if(num>0)
            ok = true;
        db.closeDB();
        return ok;
    }

    public static int checkInfo(String uname, String password) {
        DB db = new DB();
        int uid = -1;
        String sql = "SELECT `user_id` FROM `user` WHERE `user_uname` = ? AND `user_password` = ?";
        ResultSet res = db.executeQuery(sql, uname, password);
        try {
            if (res.next()) {
                uid = res.getInt("user_id");
            }
            res.close();
            db.closeDB();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return uid;
    }

    public static void printAccountInfo(int uid) {
        DB db = new DB();
        String sql = "SELECT * FROM `user` WHERE `user_id` = ?";
        ResultSet res = db.executeQuery(sql, "" + uid);
        try {
            if (res.next()) {
                String uname = res.getString("user_uname");
                String name = res.getString("user_name");
                String email = res.getString("user_email");
                String contactInfo = res.getString("user_contact_info");
                System.out.println("UID: " + uid);
                System.out.println("用户名：" + uname);
                System.out.println("用户密码：-- 不可见 --");
                System.out.println("真实姓名：" + name);
                System.out.println("电子邮件地址：" + email);
                System.out.println("联系人信息：" + contactInfo);
            } else {
                System.out.println("没有此用户!");
            }
            res.close();
            db.closeDB();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean hasUser(int uid) {
        DB db = new DB();
        String sql = "SELECT * FROM `user` WHERE `user_id` = ?";
        return Module.hasThis(uid, db, sql);
    }

    /**
     * 修改账户信息。
     * 参数：
     *  - uid(int):用户Id
     *  - field(String):要修改的字段
     *  - value(String):值
     */
    public static void updateAccountInfo(int uid, String field, String value) {
        DB db = new DB();
        String sql = "UPDATE `user` SET `"+ field +"` = ? WHERE `user_id` = ?;";
        if(db.executeUpdate(sql, value, ""+uid)>0)
            System.out.println("修改成功！");
        else
            System.out.println("修改失败！");
        db.closeDB();
    }

    public static void printAllUserAccount() {
        DB db = new DB();
        String sql = "SELECT * FROM `user`";
        ResultSet res = db.executeQuery(sql);
        try {
            if (res.next()) {
                System.out.println("用户ID\t用户名\t真实姓名\t电子邮件地址\t\t联系人信息");
                do {
                    int uid = res.getInt("user_id");
                    String uname = res.getString("user_uname");
                    String name = res.getString("user_name");
                    String email = res.getString("user_email");
                    String contactInfo = res.getString("user_contact_info");
                    System.out.printf("%d\t%s\t\t%s\t\t%s\t\t%s\n", uid, uname, name, email, contactInfo);
                } while (res.next());
            } else {
                System.out.println("没有顾客！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
